package de.recipeminer.tools;

import org.apache.log4j.Logger;
import org.jruby.CompatVersion;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;


public class RubyScriptContainer {
    public static Logger logger = Logger.getLogger(RubyScriptContainer.class);
    private static ScriptingContainer defaultInstance = null;

    /** Returns Ruby Runtime Container which is persistent over the complete program
     *  execution. It remembers loaded files and changed global variables. Local variables
     *  passed to this container persist across multiple evaulation commands.
     *
     * @return the default scripting container
     */
    public static ScriptingContainer getDefaultContainer() {
        initDefaultScriptingContainer();
        return RubyScriptContainer.defaultInstance;
    }

    /**
     * Creates a transient scripting container unaware of modifications in the default
     * container. This container does not persist local variables passed to it accross
     * different evaluations and will be subject to garbage-collection.
     *
     * @return a temporary scripting container
     */
    public static ScriptingContainer getTransientContainer() {
        ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD,
                                                              LocalVariableBehavior.TRANSIENT);
        container.setCompatVersion(CompatVersion.RUBY1_9);
        return container;
    }

    private static void initDefaultScriptingContainer() {
        if(RubyScriptContainer.defaultInstance == null) {
            defaultInstance = new ScriptingContainer(LocalContextScope.THREADSAFE,
                                                     LocalVariableBehavior.PERSISTENT);

            defaultInstance.setClassLoader(RubyScriptContainer.class.getClassLoader());
            //TODO: Find a clever method to generalize this for the JRuby-environments on different machines
            String[] loadpaths = {".",
                                  "/disk/data/home/neradis/.rvm/rubies/jruby-1.6.5/lib/ruby/site_ruby/1.9",
                                  "/disk/data/home/neradis/.rvm/rubies/jruby-1.6.5/lib/ruby/site_ruby/shared",
                                  "/disk/data/home/neradis/.rvm/rubies/jruby-1.6.5/lib/ruby/site_ruby/1.8",
                                  "/disk/data/home/neradis/.rvm/rubies/jruby-1.6.5/lib/ruby/1.9" };
            //defaultInstance.setLoadPaths(Arrays.asList(loadpaths));
            defaultInstance.setCompatVersion(CompatVersion.RUBY1_9);
            //String rubyHome = Config.getInstance().getRubyBasePath();
            //defaultInstance.setCurrentDirectory(rubyHome);
            //TODO: Find a clever method to generalize this for the JRuby-environments on different machines
            //defaultInstance.runScriptlet("");
            //defaultInstance.runScriptlet("ENV['GEM_PATH']='/disk/data/home/neradis/.rvm/gems/jruby-1.6.5@recipeminer:/disk/data/home/neradis/.rvm/gems/jruby-1.6.5@global\'");
            defaultInstance.runScriptlet("require 'main/init'");
        }
    }
}